import {
  apiGetMainProductList,
  apiAddMainProduct,
  apiUpdataMainProduct,
  apiDeleteMainProduct,
} from '@/services/api';

export default {
  namespace: 'MainProduct',
  state: {
    productCompanyData: {},
    updateDidFinish: false,
  },

  effects: {
    *getMainProductList({ payload }, { call, put }) {
      const response = yield call(apiGetMainProductList, payload);
      console.log('获取数据:', response);
      if (response) {
        yield put({
          type: 'setMainProductData',
          payload: {
            productCompanyData: response.data,
          },
        });
      }
    },
    *addMainProduct({ payload }, { call, put }) {
      console.log('payload===', payload);
      const response = yield call(apiAddMainProduct, payload);
      console.log('主营产品新增：', response);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
          // productCompanyData: response.data,
        },
      });
    },
    *updateMainProduct({ payload }, { call, put }) {
      const response = yield call(apiUpdataMainProduct, payload);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
          // productCompanyData: response?response.data:[]
        },
      });
    },
    *deleteMainProduct({ payload }, { call, put }) {
      console.log('删除id：', payload);
      const response = yield call(apiDeleteMainProduct, payload);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
          // productCompanyData: response?response.data:[]
        },
      });
      // console.log('获取数据', response);
    },
  },

  reducers: {
    setPayload(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setMainProductData(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
